package com.team.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.team.Exception.ServiceException;
import com.team.commons.dto.ActivityDto;
import com.team.commons.utils.ColaBeanUtils;
import com.team.commons.vo.ActivityVo;
import com.team.entity.Activity;
import com.team.mapper.ActivityMapper;
import com.team.result.StatusCode;
import com.team.service.ActivityService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 活动
 */
@Service
public class ActivityServiceImpl implements ActivityService {

    @Resource
    ActivityMapper activityMapper;

    @Override
    public List<ActivityVo> selectListActivityById(Long journeyId) {
        List<ActivityVo> list=new ArrayList<>();
        try {
            QueryWrapper<Activity> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq(Activity.COL_JOURNEY_ID,journeyId);
            List<Activity> activities = activityMapper.selectList(queryWrapper);
            list = ColaBeanUtils.copyListProperties(activities, ActivityVo::new);
        }catch (Exception e){
            throw new ServiceException(StatusCode.SERVER_ERROR);
        }
        return list;
    }

    @Override
    public Integer insertActivity(ActivityDto activityDto) {
        int save=0;
        try {
            Activity activity = new Activity();
            ColaBeanUtils.copyProperties(activityDto,activity);
            save = activityMapper.insert(activity);
        }catch (Exception e){
         throw new ServiceException((StatusCode.SERVER_ERROR));
        }
        if(save==0){
            throw new ServiceException((StatusCode.INTER_ERROR));
        }
        return save;
    }

    @Override
    public Integer updateActivity(ActivityDto activityDto) {
        int save=0;
        try {
            Activity activity = new Activity();
            ColaBeanUtils.copyProperties(activityDto,activity);
            save = activityMapper.updateById(activity);
        }catch (Exception e){
            throw new ServiceException((StatusCode.SERVER_ERROR));
        }
        if(save==0){
            throw new ServiceException((StatusCode.UPDATE_ERROR));
        }
        return save;
    }

    @Override
    public Integer deleteActivity(Long activityId) {
        Integer integer=0;
        try {
            integer = activityMapper.deleteActivity(activityId);
        }catch (Exception e){
            throw new ServiceException((StatusCode.SERVER_ERROR));
        }
        if(integer==0){
            throw new ServiceException((StatusCode.DELETE_ERROR));
        }
        return integer;
    }
}
